<template>
  <div class="app-container">
    <!-- 查询 -->
    <div class="filter-container">
      <el-row :gutter="0">
        <el-col :xs="24" :sm="12">
          <el-select class="filter-item" v-model="query.banji_id" placeholder="请选择班级">
            <el-option
              v-for="item in banjis"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-button class="filter-item" type="primary" @click="search()">查询</el-button>
          <el-button class="filter-item" @click="refresh()">刷新</el-button>
        </el-col>
        <el-col :xs="24" :sm="12" style="text-align:right;">
          <el-button class="filter-item" @click="outExcel()">导出</el-button>
          <el-button class="filter-item" @click="tongjiZongfen()" icon="el-icon-circle-plus">统计总分与名次</el-button>
        </el-col>
      </el-row>
    </div>
    <!-- 列表 -->
    <el-table v-loading="loading" :data="datas" :height="height" style="width: 100%" border @sort-change="sort">
      <el-table-column prop="id" label="ID" min-width="50" sortable="custom"/>
      <el-table-column prop="student_xh" label="学号" min-width="50" sortable="custom"/>
      <el-table-column prop="student_name" label="姓名" min-width="50" sortable="custom"/>
      <el-table-column prop="banji_name" label="班级" min-width="50" sortable="custom"/>
      <el-table-column v-for="(item,i) in xuekes" prop="fenshu" :label="item.xueke_name" min-width="50" align="center">
        <template slot-scope="scope">
          {{scope.row.chengji[item.xueke_id]}}
          <!-- <el-switch v-model="scope.row.ismain" :active-value="1" :inactive-value="0" disabled/> -->
        </template>
      </el-table-column>
      <el-table-column prop="zongfen" label="总分" min-width="50" sortable="custom"/>
      <el-table-column prop="mc_banji" label="班内名次" min-width="50" sortable="custom"/>
      <el-table-column prop="mc_school" label="校名次" min-width="50" sortable="custom"/>
    </el-table>
  </div>
</template>

<script>
import screenHeight from '@/utils/screen-height'
import Pagination from '@/components/Pagination'
import { getUserId, getToken } from '@/utils/auth'
import { list, info, add, edit, del,download,listBanjisByKaoshiId,listChengjiHuizong,tongjiZongfen,outExcel } from '@/api/kaoshi_chengji'

export default {
  name: 'KaoshiChengji',
  components: { Pagination },
  props: ['kaoshi_id','kaoshi_name'],
  data() {
    return {
      height: 680,
      loading: false,
      datas: [],
      count: 0,
      query: {
        page: 1,
        limit: 13,
        banji_id:''
      },
      xuekes:[],
      banjis:[],
      dialog: false,
      dialogTitle: '',
      model: {
        id:'',
        xueke_id:'',
        student_id:'',
        kaosheng_id:'',
        banji_id:'',
        kaoshi_id:'',
        fenshu_zhuguang:'',
        fenshu_keguang:'',
        mc_school:'',
        mc_banji:'',
        tscore:'',
        dengdi:'',
        fenshu:'',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
      importDialog: false,
      importDialogTitle: '',
      uploadAction: process.env.VUE_APP_BASE_API + '/chengji/KaoshiChengji/import',
      uploadHeaders: { UserId: getUserId(), Token: getToken() },
      uploadData: { user_id: getUserId(),kaoshi_id:this.kaoshi_id }
    }
  },
  created() {
    this.height = screenHeight()
    this.list()
    this.listBanjisByKaoshiId()
  },
  methods: {
    outExcel(){
      outExcel({kaoshi_id:this.kaoshi_id}).then(res=>{
        this.downloadFile1(res);
      }).catch(function (error) {
        console.log(error);
      });
    },
    // 下载文件
    downloadFile1 (data) {
      if (!data) {
        return
      }
      let blob = new Blob([data])
      let fileName = this.kaoshi_name + '成绩汇总表.xls'
      if ('download' in document.createElement('a')) { // 不是IE浏览器
        let url = window.URL.createObjectURL(blob)
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link) // 下载完成移除元素
        window.URL.revokeObjectURL(url) // 释放掉blob对象
      } else { // IE 10+
        window.navigator.msSaveBlob(blob, fileName)
      }
    },
    listBanjisByKaoshiId(){
      listBanjisByKaoshiId({kaoshi_id:this.kaoshi_id}).then(res=>{
        this.banjis = res.data;
      }).catch(()=>{
        this.loading = false
      })
    },
    // 列表
    list() {
      this.loading = true
      this.query.kaoshi_id = this.kaoshi_id
      listChengjiHuizong(this.query).then(res => {
        this.datas = res.data.kaoshengs
        this.xuekes = res.data.xuekes
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 刷新
    refresh() {
      this.query = this.$options.data().query
      this.list()
    },
    sort(sort) {
      this.query.sort_field = sort.prop
      this.query.sort_type = ''
      if (sort.order === 'ascending') {
        this.query.sort_type = 'asc'
        this.list()
      }
      if (sort.order === 'descending') {
        this.query.sort_type = 'desc'
        this.list()
      }
    },
    // 查询
    search() {
      this.query.page = 1
      this.list()
    },
    tongjiZongfen(){
      this.loading = true
      tongjiZongfen({kaoshi_id:this.kaoshi_id}).then(response => {
        this.$message.success(response.msg)
        this.list()
      }).catch(function (error) {
        console.log(error)
        this.loading = false
      });
    },
    download()
    {
      download().then(response => {
        this.downloadFile(response);
        this.$message.success(response.msg)
      }).catch(function (error) {
        console.log(error);
      });
    },
    // 下载文件
    downloadFile (data) {
      if (!data) {
        return
      }
      let blob = new Blob([data])
      let fileName = '成绩导入模板.xlsx'
      if ('download' in document.createElement('a')) { // 不是IE浏览器
        let url = window.URL.createObjectURL(blob)
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link) // 下载完成移除元素
        window.URL.revokeObjectURL(url) // 释放掉blob对象
      } else { // IE 10+
        window.navigator.msSaveBlob(blob, fileName)
      }
    }
  }
}
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
